<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />

        <!-- 放置一个 el-tree 组件 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 使用插槽展示节点内容 -->
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
          />
        </el-tree>
      </el-card>

      <!-- 放置新增部门弹层组件 -->
      <add-dept :showDialog="showDialog" :treeNode="node" />
    </div>
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools";
import AddDept from "./components/add-dept";
import { getDepartments } from "@/api/departments";
import { transListToTreeData } from "@/utils";
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: {}, // 头部的数据结构
      departs: [],
      defaultProps: {
        label: "name",
        children: "children"
      },
      showDialog: false,
      node: ""
    };
  },
  created() {
    this.getDepartments(); // 调用自身的方法
  },
  methods: {
    // 获取部门信息
    async getDepartments() {
      const result = await getDepartments();
      this.company = { name: result.companyName, manager: "负责人", id: "" };
      // this.departs = result.depts; // 需要将其转化成树形结构
      console.log(result);
      this.departs = transListToTreeData(result.depts, ""); // 因为所有子节点的 pid 都是 '' ,所以要传递一个空字符串
    },
    // 显示弹层
    addDepts(node) {
      this.showDialog = true;
      this.node = node;
    }
  }
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
